﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;

namespace WebSys.UI.OfficeFlow
{
    public class UserlistSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        DropDownList danwei, bumen;
        Button btnQuery;
        TextBox txtSeach;
        MPRepeater MPRepeater1;
        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitControl();
            if (!Page.IsPostBack)
            {
                danwei.SelectedIndexChanged += new EventHandler(danwei_SelectedIndexChanged);
                bumen.Items.Add("部门");
                danwei.Items.Add("单位");
                DataTable dt = _FlowFactoryProvider.GetDepartName();
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        danwei.Items.Add(new ListItem(dt.Rows[i][0].ToString(), dt.Rows[i][0].ToString()));
                    }

                }
                if (!string.IsNullOrEmpty(bumen.Text))
                {
                    danwei_SelectedIndexChanged(null, null);
                }
                if (!string.IsNullOrEmpty(danwei.Text))
                {
                    bumen.Text = danwei.Text;
                }
                BindData();
            }
            string strUserID = base.Page.Request["userid"];
            string action = base.Page.Request["action"];
            if (action == "del") {
                if (!string.IsNullOrEmpty(strUserID)) {
                   bool del= _FlowFactoryProvider.DeleteTableRow("st_user", "id", strUserID);
                   if (del) { Antu.Utilities.UtilitiesPage.AlertDialog("删除成功!", "window.location.href=window.location.href;"); }
                }
            }
        }
        private void InitControl()
        {
            danwei = (DropDownList)FindControl("danwei");
            bumen = (DropDownList)FindControl("bumen");
            txtSeach = (TextBox)FindControl("txtSeach");
            btnQuery = (Button)FindControl("btnQuery");
            MPRepeater1 = (MPRepeater)FindControl("MPRepeater1");
            btnQuery.Click += new EventHandler(btnQuery_Click);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected void BindData() {
            string userName = txtSeach.Text;
            string dwName = danwei.Text;
            string bmName = bumen.Text;
            DataTable dt = _FlowFactoryProvider.GetAllUser(userName, dwName, bmName);
            MPRepeater1.DataSource = dt;
            MPRepeater1.DataBind();
        }
        protected void danwei_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (danwei.Text != "单位")
            {
                
                DataTable dtbumen = _FlowFactoryProvider.GetDepartNameByDName(danwei.Text);
                bumen.Items.Clear();
                if (dtbumen.Rows.Count > 0)
                {
                    bumen.Items.Add("部门");
                    for (int i = 0; i < dtbumen.Rows.Count; i++)
                        bumen.Items.Add(dtbumen.Rows[i][0].ToString());
                }
                else
                {
                    bumen.Items.Add("部门");
                    bumen.Items.Add(danwei.Text);
                }
            }
        }


    }
}


